package com.itfreer.ui.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.dialog.DialogCloseListener;
import com.itfreer.ui.dialog.DialogResult;
import com.itfreer.ui.dialog.DomainRadioDialog;
import com.itfreer.ui.standard.IDataView;
import com.itfreer.ui.standard.IPower;

import java.util.Map;

/**
 * 定义字典单选择控件
 */
public class DomainRadioView extends LinearLayout
        implements View.OnClickListener, IDataView, IPower {

    private String fieldName;
    private Object bindData;
    private DomainRadioDialog radioDialog;
    private String dialogTitle = "单选项";
    private TextView textView;
    private String securityID;
    private boolean securityVisible=true;

    public DomainRadioView(Context context) {
        super(context);
        setView(context);
    }

    public DomainRadioView(Context context, AttributeSet attrs){
        super(context, attrs);
        setView(context);
        getAttrs(context, attrs);
    }

    public DomainRadioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setView(context);
        getAttrs(context, attrs);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        securityID = a.getString(R.styleable.SimpleView_securityID);
    }

    /**
     * 设置绑定的依据对象
     * @param name
     */
    @Override
    public void setFieldName(String name){
        this.fieldName = name;
    }

    /**
     * 获取绑定的依据对象
     * @return
     */
    @Override
    public String getFieldName(){
        return fieldName;
    }

    /**
     * 设置绑定的值
     * @param value
     */
    @Override
    public void setFieldValue(Object value){
        radioDialog.setCheckedValue(value);
        setValueText();
    }

    /**
     * 获取当前的值
     * @return
     */
    @Override
    public Object getFieldValue(){
        return radioDialog.getCheckedValue();
    }

    /**
     * 设置绑定的数据
     * @param data
     */
    @Override
    public void setBindData(Object data){
        bindData = data;
        radioDialog.setBindData(bindData);
    }

    /**
     * 获取绑定的数据
     * @return
     */
    @Override
    public Object getBindData(){
        return bindData;
    }

    /**
     * 设置标题
     * @param dialogTitle
     */
    public void setDialogTitle(String dialogTitle){
        this.dialogTitle = dialogTitle;
        radioDialog.setDialogTitle(dialogTitle);
    }

    public String getDialogTitle(){
        return dialogTitle;
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        View view = View.inflate(context, R.layout.com_itfreer_core_ui_base_inputitem, null);

        textView = (TextView)view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_textview);
        textView.setOnClickListener(this);

        com.itfreer.ui.base.TextView imageView = (com.itfreer.ui.base.TextView) view.findViewById(R.id.com_itfreer_core_ui_base_datepickerview_down);
        imageView.setOnClickListener(this);

        LayoutParams param =
                new LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(view, param);

        radioDialog = new DomainRadioDialog(this.getContext());
        radioDialog.setDialogTitle(dialogTitle);
        radioDialog.setDialogCloseListener(new DialogCloseListener() {
            @Override
            public void OnClosed(Object send, DialogResult result) {
                if (result == DialogResult.Ok) {
                    setValueText();
                }
            }
        });
    }

    protected void setValueText(){
        if(bindData instanceof Map && radioDialog.getCheckedValue()!=null){
            Map data = (Map)bindData;
            if(data.containsKey(radioDialog.getCheckedValue())){
                textView.setText(data.get(radioDialog.getCheckedValue()).toString());
            }
        }
    }

    @Override
    public void onClick(View v) {
        radioDialog.show();
    }

    /**
     * 获取权限ID
     * @return
     */
    public String getSecurityID(){
        return securityID;
    }

    /**
     * 没有权限时，设置是否可见
     * @param visible
     */
    public void setSecurityVisible(boolean visible){
        securityVisible = visible;
        if(visible){
            this.setVisibility(View.VISIBLE);
        }else{
            // 表示不可见并且不占用空间
            this.setVisibility(View.GONE);
        }
    }
}